#include "widget.h"

#include <QBoxLayout>
#include <QTimer>

#include <osgDB/ReadFile>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/SphericalManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/TrackballManipulator>
#include <osgQOpenGL/osgQOpenGLWidget>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgUtil/Optimizer>

#include <osgEarth/Config>
#include <osgEarth/GLUtils>
#include <osgEarth/MapNode>
#include <osgEarth/TerrainOptions>
#include <osgEarth/Version>
#include <osgEarth/EarthManipulator>

#if OSGEARTH_VERSION_LESS_THAN(3,0,0)
#include <osgEarth/MapNodeOptions>    
#endif

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
	resize(400, 300);
	QHBoxLayout *pLayout = new QHBoxLayout(this);
	pLayout->setMargin(0);

	osgQOpenGLWidget *pOsgW = new osgQOpenGLWidget;
	pLayout->addWidget(pOsgW);
	connect(pOsgW, SIGNAL(initialized()), this, SLOT(initOsg()));
}

Widget::~Widget()
{
}

void Widget::initOsg()
{
	osgViewer::Viewer *pViewer = ((osgQOpenGLWidget *)sender())->getOsgViewer();
	//pViewer->setCameraManipulator(new osgGA::TrackballManipulator());
// 	pViewer->setCameraManipulator(new osgEarth::Util::EarthManipulator());
// 	osg::Node *pNode = osgDB::readNodeFile("boxman.osg");
	osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
	keyswitchManipulator->addMatrixManipulator('1', "Trackball", new osgGA::TrackballManipulator());
	keyswitchManipulator->addMatrixManipulator('2', "Flight", new osgGA::FlightManipulator());
	keyswitchManipulator->addMatrixManipulator('3', "Drive", new osgGA::DriveManipulator());
	keyswitchManipulator->addMatrixManipulator('4', "Terrain", new osgGA::TerrainManipulator());
	keyswitchManipulator->addMatrixManipulator('5', "Orbit", new osgGA::OrbitManipulator());
	keyswitchManipulator->addMatrixManipulator('6', "FirstPerson", new osgGA::FirstPersonManipulator());
	keyswitchManipulator->addMatrixManipulator('7', "Spherical", new osgGA::SphericalManipulator());
	pViewer->setCameraManipulator(keyswitchManipulator.get());
	// add the state manipulator
	pViewer->addEventHandler(new osgGA::StateSetManipulator(pViewer->getCamera()->getOrCreateStateSet()));
	// add the stats handler
	pViewer->addEventHandler(new osgViewer::StatsHandler);

#if OSGEARTH_VERSION_LESS_THAN(3,0,0)
	osgEarth::Config c;
	c.add("elevation_smoothing", false);
	osgEarth::TerrainOptions to(c);
	osgEarth::MapNodeOptions defMNO;
	defMNO.setTerrainOptions(to);
	osg::ref_ptr<osgDB::Options> options = new osgDB::Options();
	options->setPluginStringData("osgEarth.defaultOptions", defMNO.getConfig().toJSON());
	osg::Node *pNode = osgDB::readNodeFile("simple.earth", options);
#else
    osg::Node *pNode = osgDB::readNodeFile("simple.earth");
#endif

	osgUtil::Optimizer optimizer;
	optimizer.optimize(pNode);
	pViewer->setSceneData(pNode);
}
